import json

from langchain_community.document_loaders import PyPDFLoader

from langchain_text_splitters import RecursiveCharacterTextSplitter

from langchain_community.embeddings import DashScopeEmbeddings


from langchain_core.vectorstores import InMemoryVectorStore

file_path = "./罗拉API接口.pdf"
loader = PyPDFLoader(file_path)

docs = loader.load()

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=500, chunk_overlap=200, add_start_index=True
)
all_splits = text_splitter.split_documents(docs)
# print(len(all_splits))
# for all_split in all_splits:
#     print(all_split)


embeddings = DashScopeEmbeddings(
    # model="multimodal-embedding-v1",
    model="text-embedding-v1",
    dashscope_api_key="sk-d16b46d66abb45bb960bd9c57804e2f9",
    # other params...
)

vector_1 = embeddings.embed_query(all_splits[0].page_content)
vector_2 = embeddings.embed_query(all_splits[1].page_content)

vector_store = InMemoryVectorStore(embeddings)
vector_store.add_documents(all_splits)

# results = vector_store.similarity_search_with_score(
#     # "罗拉api签名的方式是什么"
#     "我是邢冬阳"
# )

# doc, score = results[0]
# print(f"Score: {score}\n")

embedding = embeddings.embed_query("我是邢冬阳")

results = vector_store.similarity_search_by_vector(embedding)
print(results[0])

